package com.jy.event.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by Joe
 * Date: 2011-5-2 22:04:45
 */
@Slf4j
public final class FileUtils {
    public static String readLineInClassPathResourceFile(int index, String path) {
        InputStream ins = null;
        BufferedReader reader = null;
        try {
            ClassPathResource resource = new ClassPathResource(path);
            ins = resource.getInputStream();

            reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            String line = "";
            int i = 0;
            while ((line = reader.readLine()) != null) {
                if (i == index) return line;
                i++;
            }
        } catch (Exception ignored) {

        } finally {
            try {
                if (reader != null) {
                    reader.close();
                    reader = null;
                }
                if (ins != null) {
                    ins.close();
                    ins = null;
                }
            } catch (IOException ignored) {

            }
        }
        return null;
    }

    public static boolean isExistInClassPathResourceFile(String text, String path) {
        InputStream ins = null;
        BufferedReader reader = null;
        try {
            ClassPathResource resource = new ClassPathResource(path);
            ins = resource.getInputStream();

            reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            String line = "";
            int i = 0;
            while ((line = reader.readLine()) != null) {
                if (text.equals(line.trim())) {
                    return true;
                }
                i++;
            }
        } catch (Exception ignored) {

        } finally {
            try {
                if (reader != null) {
                    reader.close();
                    reader = null;
                }
                if (ins != null) {
                    ins.close();
                    ins = null;
                }
            } catch (IOException ignored) {

            }
        }
        return false;
    }

}
